%%
% Modified by Jeff McKinstry, 10-28-09
% Allows passing in an array of x values for the cross-section of the Gabor
% function.  This allows you to set the phase and the number of pixels in
% your output array.  
% 
% Sample usage:
% imagesc(gaborfilter1(-2*pi:pi/4:2*pi,pi,10*pi,pi/4)),colormap(gray)
%
%The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
%modulated by a sinusoid (with centre frequencies U and V along x and y-axes respectively) 
%described by the following equation
%%
%                            -1     x' ^     y'  ^             
%%% G(x,y,theta,f) =  exp ([----{(----) 2+(----) 2}])*cos(2*pi*f*x');
%                             2    sx'       sy'
%%% x' = x*cos(theta)+y*sin(theta);
%%% y' = y*cos(theta)-x*sin(theta);

%% Describtion :

%% one_d_fun : Input array of x values for the cross-section of the Gabor function; if n==length(one_d_fun) then G is nXn.
%% Sx & Sy : Variances along x and y-axes respectively
%% theta : The orientation of Gabor filter

%% G : The output filter as described above



%%  Author : Ahmad poursaberi  e-mail : a.poursaberi@ece.ut.ac.ir
%%          Faulty of Engineering, Electrical&Computer Department,Tehran
%%          University,Iran,June 2004

function G = gaborfilter(pixels,Sx,Sy,theta,cycles, phase_percent);

xphase = phase_percent*cycles*pi*cos(theta);
yphase = phase_percent*cycles*pi*sin(theta);

step = 2*pi/((pixels-1)/cycles);
one_d_xfun = (-cycles*pi+xphase):step:(cycles*pi+xphase);
one_d_yfun = (-cycles*pi+yphase):step:(cycles*pi+yphase);


% rotate
[x y] = meshgrid(one_d_xfun,one_d_yfun);
xPrime = x * cos(theta) + y * sin(theta);
%imagesc(xPrime)
yPrime = y * cos(theta) - x * sin(theta);
%figure;imagesc(yPrime)
G = exp(-.5*((xPrime/Sx).^2+(yPrime/Sy).^2)).*cos(xPrime);

%G=double(real(G));